<?php
/**
 * @title index.php
 * @description Simple status updater
 */
include_once 'client/facebook.php';

// some basic library functions
include_once 'lib.php';

// this defines some of your basic setup
include_once 'config.php';

$facebook = new Facebook($facebook_config['api_key'],
$facebook_config['secret']);
	
$user = $facebook->require_login();

if(isset($_REQUEST['status'])){
    // check permissions
    $has_permission = $facebook->api_client->call_method(
        "facebook.users.hasAppPermission",
        array("ext_perm"=>"status_update")
    );
    if($has_permission){
        //update status
        $facebook->api_client->call_method(
            "facebook.users.setStatus", array("status" => $_REQUEST['status'])
        );
		        // grab the status
        $fql = "SELECT status FROM user WHERE uid=" . $user;
        $query_result = $facebook->api_client->fql_query($fql);
        $status = $query_result[0]['status']['message'];
        echo("<p>Your status is now: <strong>" . $status . "</strong></p>");
    }else {
        $url = '<a href="http://www.facebook.com/authorize.php?api_key=';
        $url += $api_key . '&v=1.0&ext_perm=status_update">Click here</a>';
        echo('<p style="font-size:14px;"> ');
        echo('D\'oh...it doesn\'t look like you have permissions to change your
            status. ' . $url . ' to add the permissions to update your
            status.');
        echo('</p>');
    }
    exit;
}
?>
<div style="padding:5px;">
    <h1>
        Hello
        <fb:name uid="<?= $user?>" firstnameonly="true" capitalize="true" />
    </h1>
    <p>What's your status?</p>
    <form action="help.php" name="status" id="status">
  <input type="text" name="status" value="confused" />
        <input type="submit" clickrewriteid="curr_status"
            clickrewriteurl="http://googlewala.com/fb8/" />
    </form>
  <div id="curr_status"></div>
</div>